<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-if="formThead.id" v-model="listQuery.id" style="width: 200px;" class="filter-item" placeholder="id" clearable @keyup.enter.native="handleFilter"/>
      <el-input v-if="formThead.nickName" v-model="listQuery.nickName" style="width: 200px;" class="filter-item" placeholder="昵称" clearable @keyup.enter.native="handleFilter"/>
      <el-input v-if="formThead.realName" v-model="listQuery.realName" style="width: 200px;" class="filter-item" placeholder="真实姓名" clearable @keyup.enter.native="handleFilter"/>
      <el-input v-if="formThead.merchant_name" v-model="listQuery.merchant_name" style="width: 200px;" class="filter-item" placeholder="来源商家" clearable @keyup.enter.native="handleFilter"/>
      <div v-if="formThead.createTime" class="filter-item">
        <el-date-picker v-model="listQuery.createTime" type="datetime" placeholder="注册时间" clearable @keyup.enter.native="handleFilter"/>
      </div>
      <div v-if="formThead.gender" class="filter-item">
        <el-select v-model="listQuery.gender" placeholder="性别" clearable @keyup.enter.native="handleFilter">
          <el-option v-for="op in select_gender" :key="op.value" :label="op.label" :value="op.value"/>
        </el-select>
      </div>
      <div v-if="formThead.status" class="filter-item">
        <el-select v-model="listQuery.status" placeholder="状态" clearable @keyup.enter.native="handleFilter">
          <el-option v-for="op in select_status" :key="op.value" :label="op.label" :value="op.value"/>
        </el-select>
      </div>
      <el-input v-if="formThead.phone" v-model="listQuery.phone" style="width: 200px;" class="filter-item" placeholder="电话" clearable @keyup.enter.native="handleFilter"/>

      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <!--<addPage @getLists="getLists"/>-->
    </div>

    <div class="filter-container">
      <el-checkbox v-model="formThead.id" label="id">id</el-checkbox>
      <el-checkbox v-model="formThead.avatarUrl" label="头像">头像</el-checkbox>
      <el-checkbox v-model="formThead.nickName" label="昵称">昵称</el-checkbox>
      <el-checkbox v-model="formThead.gender" label="性别">性别</el-checkbox>
      <el-checkbox v-model="formThead.realName" label="真实姓名">真实姓名</el-checkbox>
      <el-checkbox v-model="formThead.phone" label="电话">电话</el-checkbox>
      <el-checkbox v-model="formThead.merchant_name" label="来源商家">来源商家</el-checkbox>
      <el-checkbox v-model="formThead.createTime" label="注册时间">注册时间</el-checkbox>
      <el-checkbox v-model="formThead.status" label="状态">状态</el-checkbox>
    </div>

    <el-table v-loading="listLoading" :key="tableKey" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column v-if="formThead.id" label="id" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.nickName" label="昵称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.avatarUrl" label="头像" align="center">
        <template slot-scope="scope" >
          <span><img :src="scope.row.avatarUrl" alt="" height="120" @click="bigImage(scope.row.avatarUrl)"></span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.gender" label="性别" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.gender=='1'">男</span>
          <span v-if="scope.row.gender=='0'">女</span>
          <span v-if="scope.row.gender=='2'">保密</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.realName" label="真实姓名" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.realName }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.phone" label="电话" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.createTime" label="注册时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | formatDate }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.status" align="center" label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status=='0'"><i class="el-icon-circle-check" style="color: #67C23A"/></span>
          <span v-else><i class="el-icon-circle-close" style="color: #ebb563"/></span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.merchant_name" label="来源商家" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.merchant_name }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.price" label="总支付金额" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <userDetail :row_data="scope.row.id" @getLists="getLists"/>
          <orderList :row_data="scope.row.id" @getLists="getLists"/>
        </template>
      </el-table-column>
    </el-table>
    <!--大图弹窗-->
    <transition name="el-zoom-in-top">
      <div v-show="bigImageSrc !== null" class="bigImg" @click="bigImage">
        <img :src="bigImageSrc" style="width:auto; height:400px; border-radius:0px;">
      </div>
    </transition>
    <!--页码-->
    <div class="pagination-container">
      <el-pagination :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
// import { addPageVisitNum } from '@/utils/addPageVisitNum.js'
import { getList, forbidden, invoke } from '@/api/user/user'
import store from '@/store'
import waves from '@/directive/waves' // 水波纹指令
import addPage from './addPage'
import editPage from './editPage'
import { parseTime } from '@/utils/index'
import userDetail from './userDetail'
import orderList from './orderList'

export default {
  name: 'ComplexTable',
  directives: {
    waves
  },
  components: { addPage, editPage, userDetail, orderList },
  filters: {
    formatDate: function(time) {
      if (!time) {
        return ''
      }
      return parseTime(time)
    }
  },

  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      user_details: [],
      order_lists: [],
      user: [],
      listLoading: true,
      bigImageSrc: null,
      select_gender: [{
        value: '1',
        label: '男'
      }, {
        value: '0',
        label: '女'
      }, {
        value: '2',
        label: '保密'
      }],
      select_status: [
        { label: '启用', value: '0' },
        { label: '禁用', value: '1' }
      ],
      listQuery: {
        id: undefined,
        openid: undefined,
        avatarUrl: undefined,
        nickName: undefined,
        gender: undefined,
        country: undefined,
        province: undefined,
        language: undefined,
        merchant_name: undefined,
        price: undefined,
        createTime: undefined,
        status: undefined,
        phone: undefined,
        realName: undefined,
        page: 1,
        limit: 20
      },
      formThead: {
        id: true,
        openid: true,
        avatarUrl: true,
        nickName: true,
        gender: true,
        country: true,
        province: true,
        language: true,
        merchant_name: true,
        price: true,
        createTime: true,
        status: true,
        phone: true,
        realName: true
      }
    }
  },
  created() {
    this.getLists()
    // addPageVisitNum(this.$router.currentRoute.fullPath, '用户列表')
  },
  methods: {
    invoke(id) {
      invoke(store.getters.token, { id: id }).then(res => {
        this.$message({
          message: '启用',
          type: 'success'
        })
        this.getLists()
      })
    },
    forbidden(id) {
      forbidden(store.getters.token, { id: id }).then(res => {
        this.$message({
          message: '禁用',
          type: 'warning'
        })
        this.getLists()
      })
    },
    getLists() {
      this.listLoading = true
      getList(store.getters.token, this.listQuery).then(response => {
        for (let i = 0; i < Object.keys(response.data.list.data).length; i++) {
          response.data.list.data[i].createTime *= 1000
        }
        this.list = response.data.list.data
        this.total = response.data.list.total
        this.listLoading = false
      })
    },
    bigImage(src) {
      if (src > 0) {
        this.set(this, 'bigImageSrc', src)
      } else {
        this.set(this, 'bigImageSrc', null)
      }
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getLists()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getLists()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getLists()
    }
  }
}
</script>
